package com.wk.myhandle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;

import java.time.Clock;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private MyHandler handler ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyLooper.prepare();
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyLooper.prepare();
                 handler = new MyHandler(){
                    @Override
                    public void handleMessage(MyMessage myMessage) {
                        super.handleMessage(myMessage);
                        String msg =  myMessage.what;
                        String reslut =String.format(Locale.getDefault(),"线程%d收到%s。" ,Thread.currentThread().getId() ,msg);
                        Log.i("testTag" ,reslut ) ;
                    }
                };
                MyLooper.loop();
            }
        }).start();


        SystemClock.sleep(1000); // 等待子线程 handler 创建完毕

        new Thread(new Runnable() {
            @Override
            public void run() {
                MyMessage message = new MyMessage();
                message.what = String.format(Locale.getDefault(),"我是线程%d发送的信息" ,Thread.currentThread().getId() );
                handler.sendMessage(message);
            }
        }).start(); ;

    }



}
